<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- <el-form>
          <h3>{{ form.name }}</h3>
          <el-form-item>
            <el-select v-model="form.province">
              <el-option
                v-for="item in list"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form> -->

        <!-- 搜索nav栏 -->
        <el-row :gutter="10">
          <el-col :span="18">
            <!-- 搜索框 -->
            <el-col :span="6">
              <el-form ref="subjectFrom" :model="queryList" label-width="80px">
                <el-form-item label="学科名称">
                  <el-input
                    @clear="getSubjectsList"
                    v-model="queryList.subjectName"
                    style="height: 32px"
                  ></el-input>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="18">
              <el-button size="small" class="colInput" plain @click="clearSub"
                >清除</el-button
              >
              <el-button
                size="small"
                class="colInput"
                type="primary"
                @click="getSubjectsList"
                >搜索</el-button
              >
            </el-col>
          </el-col>
          <el-col :span="6">
            <el-button
              class="colright"
              size="mini"
              type="success"
              icon="el-icon-edit"
              @click="addSubjects"
              >新增学科</el-button
            >
          </el-col>
        </el-row>
        <!-- 消息文案 -->
        <el-alert
          :title="`数据一共 ${counts}  条`"
          :closable="false"
          type="info"
          show-icon
        >
        </el-alert>

        <!-- 表格区域 -->
        <template>
          <el-table :data="list" stripe style="width: 100%; margin-top: 20px">
            <el-table-column
              fixed="left"
              type="index"
              label="序号"
              width="80px"
            >
            </el-table-column>
            <el-table-column prop="subjectName" label="学科名称">
            </el-table-column>
            <el-table-column prop="username" label="创建者"> </el-table-column>
            <el-table-column prop="addDate" label="创建日期">
              <template #default="{ row }">
                {{ addDate(row.addDate) }}
              </template>
            </el-table-column>
            <el-table-column prop="isFrontDisplay" label="前台是否显示">
              <template #default="{ row }">
                <!-- {{ row.isFrontDisplay === 1 ? '是' : '否' }} -->
                {{ hireEnum[row.isFrontDisplay] }}
              </template>
            </el-table-column>
            <el-table-column prop="twoLevelDirectory" label="二级目录">
            </el-table-column>
            <el-table-column prop="tags" label="标签"> </el-table-column>
            <el-table-column prop="totals" label="题目数量"> </el-table-column>
            <el-table-column fixed="right" label="操作" width="250px">
              <template slot-scope="scope">
                <el-button size="small" type="text" @click="goDirectorys(scope)"
                  >学科分类</el-button
                >
                <el-button size="small" type="text" @click="goTags(scope)"
                  >学科标签</el-button
                >
                <el-button
                  size="small"
                  type="text"
                  @click="editSubList(scope.row)"
                  >修改</el-button
                >
                <el-button
                  size="small"
                  type="text"
                  @click="deleteSub(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>

        <!-- 分页 -->
        <pagination-add
          :page="queryList.page"
          :pagesize="queryList.pagesize"
          :counts="counts"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        />
      </el-card>
      <!-- 新增学科弹窗 -->
      <subjects-add
        :addDialogVisible="addDialogVisible"
        @close-dialog="closeDialog"
        @get-subjects-list="getSubjectsList"
      ></subjects-add>

      <!-- 修改的弹框 -->
      <el-dialog
        title="修改学科"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editResetSub"
      >
        <!-- 主体区域 -->
        <el-row>
          <el-col>
            <el-form
              :model="editSubjectsFrom"
              :rules="editSubjectsFromRules"
              ref="editSubjectsFromRef"
              label-width="80px"
              class="demo-ruleForm"
            >
              <el-form-item label="学科名称" prop="subjectName">
                <el-input v-model="editSubjectsFrom.subjectName"></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <span class="subspan">是否显示</span>
          <el-switch
            v-model="editSubjectsFrom.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-row>
        <!-- 按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editSubFrom">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Subject from '../../api/hmmm/subjects'
import PaginationAdd from '../components/pagination/pagination-add'
import dayjs from 'dayjs'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'Subjects',
  components: {
    PaginationAdd,
    SubjectsAdd
  },
  data () {
    return {
      // 修改的id
      userId: '',
      // 修改的默认表单值
      editSubjectsFrom: {
        subjectName: '',
        isFrontDisplay: ''
      },
      // 修改的验证
      editSubjectsFromRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 修改学科的弹框显示
      editDialogVisible: false,
      // 获取学科列表的请求参数
      queryList: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      // 前台是否显示的枚举
      hireEnum: {
        1: '是',
        0: '否'
      },
      // 学科的数据列表
      list: [], // 从接口处取回

      // 学科列表总条数
      counts: 0,
      // 控制新增学科的显示与隐藏
      addDialogVisible: false
      // form: {
      //   name: 'abc',
      //   province: '潜江' // 从接口处取回
      // }
    }
  },
  created () {
    this.getSubjectsList()
    // setTimeout(() => {
    //   // ajax
    //   // const res = {
    //   //   name: 'abc',
    //   //   list: ['潜江', '滨海', '北京'],
    //   //   province: '北京'
    //   // }
    //   // this.list = res.list
    //   // this.form.name = res.name
    //   // this.form = { name: res.name }
    //   // this.$set(this.form, 'province', res.province)
    //   // this.form.province = res.province
    // })
  },
  methods: {
    goDirectorys (scope) {
      this.$router.push({
        path: './directorys',
        query: {
          id: scope.id
        }
      })
    },
    goTags (scope) {
      this.$router.push({
        path: './tags',
        query: {
          id: scope.id
        }
      })
    },
    // 删除学科
    async deleteSub (row) {
      const configRuselt = await this.$confirm(
        '此操作将永久删除该学科, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (configRuselt !== 'confirm') {
        return this.$message('您取消了删除操作!')
      }
      try {
        const { data } = await Subject.remove(row)
        console.log(data)
        this.$message.success('删除成功!')
        this.getSubjectsList()
      } catch (e) {
        this.$message.error('删除失败!')
      }
    },
    // 修改学科
    async editSubFrom () {
      if (this.editSubjectsFrom.isFrontDisplay === true) {
        this.editSubjectsFrom.isFrontDisplay = 1
      } else {
        this.editSubjectsFrom.isFrontDisplay = 0
      }
      try {
        await Subject.update({
          id: this.userId,
          subjectName: this.editSubjectsFrom.subjectName,
          isFrontDisplay: this.editSubjectsFrom.isFrontDisplay
        })
        this.$message.success('修改学科成功')
        this.editDialogVisible = false
        this.getSubjectsList()
      } catch (e) {
        this.$message.error('修改学科失败')
      }
    },
    // 修改清空
    editResetSub () {
      this.$refs.editSubjectsFromRef.resetFields()
    },
    // 点击修改显示弹框
    editSubList (info) {
      this.editSubjectsFrom.subjectName = info.subjectName
      if (info.isFrontDisplay === 1) {
        this.editSubjectsFrom.isFrontDisplay = true
      } else {
        this.editSubjectsFrom.isFrontDisplay = false
      }
      this.userId = info.id
      this.editDialogVisible = true
    },
    // 点击新增学科按钮，显示弹窗
    addSubjects () {
      this.addDialogVisible = true
    },
    // 关闭弹窗的自定义事件的回调函数
    closeDialog () {
      // console.log('收到子组件的通知')
      // 触发自定义事件，通知父组件关闭弹窗
      this.addDialogVisible = false
    },
    // 点击清除按钮清空input输入框
    clearSub () {
      this.queryList.subjectName = ''
      this.getSubjectsList()
    },
    // 获取学科的列表数据
    async getSubjectsList () {
      const { data } = await Subject.list(this.queryList)
      console.log(data)
      this.list = data.items
      this.counts = data.counts
    },
    // 日期转换的方法
    addDate (dateValue) {
      return dayjs(dateValue).format('YYYY-MM-DD hh:mm:ss')
    },
    // 一页多少条
    handleSizeChange (newSize) {
      this.queryList.pagesize = newSize
      this.getSubjectsList()
    },
    // 多少页
    handleCurrentChange (newPage) {
      this.queryList.page = newPage
      this.getSubjectsList()
    }
  }
}
</script>

<style scoped lang="scss">
.subspan {
  font-size: 14px;
  font-weight: 700;
  margin-right: 10px;
  margin-left: 10px;
}
</style>
